/**
 * @Title: stationController.java
 * @Package tt.dz.mvc.controller.sys
 * @author pengyu
 * @date 2015年12月17日 上午10:21:55
 * @version V1.0
 */
package tt.dz.mvc.controller.sys;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import tt.dz.entity.base.JsonForList;
import tt.dz.entity.station.OmsStationExternal;
import tt.dz.service.sys.StationExternalService;
import tt.dz.util.StringUtil;

@Controller
public class ExternalChargerController {
	@Autowired
	StationExternalService stationExternalService;
	// 2.第三方站点查询接口
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getExternalStationList")
	@ResponseBody
	public JsonForList getExternalStationList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//只能管理员才能操作外部站点
		JsonForList json = new JsonForList();
		HttpSession session = request.getSession(); 
		String login_entity_type = (String) session.getAttribute("login_entity_type"); 
		if(!StringUtil.isNumeric(login_entity_type)){
			json.setCode("400");
			json.setMsg("only platform manager can getExternalStationList ");
			return json;
		}
		OmsStationExternal station = new OmsStationExternal();
		//只按照名字和省、市、区查询
		String station_name = request.getParameter("station_name");// 模糊
		if (StringUtil.isNotBlank(station_name)) {
			station.setStation_name(station_name);
		}
		String district_code = request.getParameter("district_code");// 模糊
		if (StringUtil.isNotBlank(district_code)) {
			station.setDistrict_code(district_code);
		}
		String valid_flag = request.getParameter("valid_flag");
		if (StringUtils.isNumeric(valid_flag)) {
			station.setValid_flag(Integer.valueOf(valid_flag));
		}else{
			station.setValid_flag(1);
		}
		return stationExternalService.getStationList(station);
	}
	
	// 15.第三方站点新增接口
	@SuppressWarnings("rawtypes")
	@RequestMapping("/addExternalStation")
	@ResponseBody
	public JsonForList addExternalStation(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//只能管理员才能操作外部站点
		JsonForList jsonf = new JsonForList();
		HttpSession session = request.getSession(); 
		String login_entity_type = (String) session.getAttribute("login_entity_type"); 
		if(!StringUtil.isNumeric(login_entity_type)){
			jsonf.setCode("400");
			jsonf.setMsg("only platform manager can getExternalStationList ");
			return jsonf;
		}
		JsonForList json = new JsonForList();
		String create_oper_id = request.getParameter("login_oper_id");//做假数据时用0000000000
		if(StringUtil.isNotBlank(create_oper_id)){
			create_oper_id = (String) session.getAttribute("oper_id"); 
		}
		OmsStationExternal station = new OmsStationExternal();
		String station_name = request.getParameter("station_name");
		String oper_merchant_id = request.getParameter("oper_merchant_id");// 充电运营服务商ID
		String district_code = request.getParameter("district_code");// 区县代码
		if(StringUtil.isBlank(station_name)||StringUtil.isBlank(oper_merchant_id)||StringUtil.isBlank(district_code)){
			json.setCode("400");
			json.setMsg("station_name or oper_merchant_id or district_code  null");
			return json;
		}
		//查询district_code开头的站点，生成一个新ID。
		OmsStationExternal station_query = new OmsStationExternal();
		station_query.setOper_merchant_id(oper_merchant_id);
		station_query.setDistrict_code(district_code);
		String station_id_new = stationExternalService.getNewStationId(station_query);
		station.setStation_id(station_id_new);
		station.setStation_name(station_name);
		station.setOper_merchant_id(oper_merchant_id);
		station.setDistrict_code(district_code);
		String  source_id_str = request.getParameter("source_id");//数据来源序列号,数据来源表id
		if(StringUtil.isNotBlank(source_id_str)){
			Integer source_id = Integer.valueOf(source_id_str);
			station.setSource_id(source_id);
		}
		String org_station_id = request.getParameter("org_station_id");//原始站级编号
		if(StringUtil.isNotBlank(org_station_id)){
			station.setOrg_station_id(org_station_id);
		}
		String current_state_str = request.getParameter("current_state");//当前状态
		if(StringUtil.isNotBlank(current_state_str)){
			Integer current_state = Integer.valueOf(current_state_str);
			station.setCurrent_state(current_state);
		}
		String circle = request.getParameter("circle");// 商圈/楼盘
		station.setCircle(circle);
		/*String coordtype_str = request.getParameter("coordtype");		
		if(StringUtil.isNotBlank(coordtype_str)){
			Integer coordtype = Integer.valueOf(coordtype_str);// 地图类型
			station.setCoordtype(coordtype);
		}
		String coordinate = request.getParameter("coordinate");// 坐标
		station.setCoordinate(coordinate);*/
		String parking_type = request.getParameter("parking_type");// 停车处
		station.setParking_type(parking_type);
		String parking_addr = request.getParameter("parking_addr");// 停车处大体位置
		station.setParking_addr(parking_addr);
		String photo = request.getParameter("photo");// 环境照片URL路径
		station.setPhoto(photo);
		String total_charger_str = request.getParameter("total_charger");// 总电桩数
		if(StringUtil.isNotBlank(total_charger_str)){
			Integer total_charger = Integer.valueOf(total_charger_str);
			station.setTotal_charger(total_charger);
		}
		String establish_time_date = request.getParameter("establish_time");// 建立时间
		if(StringUtil.isValidDate(establish_time_date)){
		       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		       try {
		      // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
		          format.setLenient(false);
		          station.setEstablish_time(format.parse(establish_time_date));
		       } catch (ParseException e) {
		           e.printStackTrace();
		       } 
		}
		String station_type_str = request.getParameter("station_type");// 站点类型
		if(StringUtil.isNotBlank(station_type_str)){
			Integer station_type = Integer.valueOf(station_type_str);
			station.setStation_type(station_type);
		}
		String is_allow_open = request.getParameter("is_allow_open");//  是否对外开放
		if(StringUtil.isNotBlank(is_allow_open)){
			Integer is_allow_open_int = Integer.valueOf(is_allow_open);
			station.setIs_allow_open(is_allow_open_int);
		}
		String address = request.getParameter("address");// 详细地址
		station.setAddress(address);
		String direct_num_str = request.getParameter("direct_num");//   直流桩数量
		if(StringUtil.isNotBlank(direct_num_str)){
			Integer direct_num = Integer.valueOf(direct_num_str);
			station.setDirect_num(direct_num);
		}
		String alternate_num_str = request.getParameter("alternate_num");//    交流桩数量
		if(StringUtil.isNotBlank(alternate_num_str)){
			Integer alternate_num = Integer.valueOf(alternate_num_str);
			station.setAlternate_num(alternate_num);
		}
		String tel_no = request.getParameter("tel_no");// 电话
		station.setTel_no(tel_no);
		String describes = request.getParameter("describes");// 站点描述
		station.setDescribes(describes);		
		
		String discover_user_id = request.getParameter("discover_user_id");//    
		if(StringUtil.isNotBlank(discover_user_id)){
			station.setDiscover_user_id(discover_user_id);
		}
		String pay_manner = request.getParameter("pay_manner");//     
		if(StringUtil.isNotBlank(pay_manner)){
			station.setPay_manner(pay_manner);
		}
		station.setValid_flag(1);
		return stationExternalService.addStation(station);
	}
	
	// 16.第三方站点修改接口
	@SuppressWarnings("rawtypes")
	@RequestMapping("/updateStationExternal")
	@ResponseBody
	public JsonForList updateStationExternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//只能管理员才能操作外部站点
		JsonForList json = new JsonForList();
		HttpSession session = request.getSession(); 
		String login_entity_type = (String) session.getAttribute("login_entity_type"); 
		String login_super_flag = (String) session.getAttribute("login_super_flag"); 
		if(!StringUtil.isNumeric(login_entity_type)){
			json.setCode("400");
			json.setMsg("only platform manager can getExternalStationList ");
			return json;
		}
		OmsStationExternal station = new OmsStationExternal();
		String station_id = request.getParameter("station_id");
		if(StringUtil.isBlank(station_id)){
			json.setCode("400");
			json.setMsg("station_id  null");
			return json;
		}
		OmsStationExternal omsStation_check = new OmsStationExternal();
		omsStation_check.setStation_id(station_id);
		JsonForList jsonforlist  = new JsonForList();
//		if(StringUtil.isNumeric(login_entity_type) && login_entity_type.equals("0")){
		if(StringUtil.isNumeric(login_entity_type) && login_super_flag.equals("1")){	
			jsonforlist = stationExternalService.getStationList(omsStation_check);
		}else{
			json.setCode("400");
			json.setMsg("please login");
			return json;
		}
		if(jsonforlist.getList()==null){
			json.setCode("400");
			json.setMsg(" station_id not exist ");
			return json;
		}
		station.setStation_id(station_id);	
		String station_name = request.getParameter("station_name");
		station.setStation_name(station_name);
		String oper_merchant_id = request.getParameter("oper_merchant_id");// 充电运营服务商ID
		station.setOper_merchant_id(oper_merchant_id);
		String district_code = request.getParameter("district_code");// 区县代码
		station.setDistrict_code(district_code);
		String  source_id_str = request.getParameter("source_id");//数据来源序列号,数据来源表id
		if(StringUtil.isNotBlank(source_id_str)){
			Integer source_id = Integer.valueOf(source_id_str);
			station.setSource_id(source_id);
		}
		String org_station_id = request.getParameter("org_station_id");//原始站级编号
		if(StringUtil.isNotBlank(org_station_id)){
			station.setOrg_station_id(org_station_id);
		}
		String current_state_str = request.getParameter("current_state");//当前状态
		if(StringUtil.isNotBlank(current_state_str)){
			Integer current_state = Integer.valueOf(current_state_str);
			station.setCurrent_state(current_state);
		}
		String circle = request.getParameter("circle");// 商圈/楼盘
		station.setCircle(circle);
		/*String coordtype_str = request.getParameter("coordtype");		
		if(StringUtil.isNotBlank(coordtype_str)){
			Integer coordtype = Integer.valueOf(coordtype_str);// 地图类型
			station.setCoordtype(coordtype);
		}
		String coordinate = request.getParameter("coordinate");// 坐标
		station.setCoordinate(coordinate);*/
		String parking_type = request.getParameter("parking_type");// 停车处
		station.setParking_type(parking_type);
		String parking_addr = request.getParameter("parking_addr");// 停车处大体位置
		station.setParking_addr(parking_addr);
		String photo = request.getParameter("photo");// 环境照片URL路径
		station.setPhoto(photo);
		String total_charger_str = request.getParameter("total_charger");// 总电桩数
		if(StringUtil.isNotBlank(total_charger_str)){
			Integer total_charger = Integer.valueOf(total_charger_str);
			station.setTotal_charger(total_charger);
		}
		String establish_time_date = request.getParameter("establish_time");// 建立时间
		if(StringUtil.isValidDate(establish_time_date)){
		       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		       try {
		      // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
		          format.setLenient(false);
		          station.setEstablish_time(format.parse(establish_time_date));
		       } catch (ParseException e) {
		           e.printStackTrace();
		       } 
		}
		String station_type_str = request.getParameter("station_type");// 站点类型
		if(StringUtil.isNotBlank(station_type_str)){
			Integer station_type = Integer.valueOf(station_type_str);
			station.setStation_type(station_type);
		}
		String is_allow_open = request.getParameter("is_allow_open");//  是否对外开放
		if(StringUtil.isNotBlank(is_allow_open)){
			Integer is_allow_open_int = Integer.valueOf(is_allow_open);
			station.setIs_allow_open(is_allow_open_int);
		}
		String address = request.getParameter("address");// 详细地址
		station.setAddress(address);
		String direct_num_str = request.getParameter("direct_num");//   直流桩数量
		if(StringUtil.isNotBlank(direct_num_str)){
			Integer direct_num = Integer.valueOf(direct_num_str);
			station.setDirect_num(direct_num);
		}
		String alternate_num_str = request.getParameter("alternate_num");//    交流桩数量
		if(StringUtil.isNotBlank(alternate_num_str)){
			Integer alternate_num = Integer.valueOf(alternate_num_str);
			station.setAlternate_num(alternate_num);
		}
		String tel_no = request.getParameter("tel_no");// 电话
		station.setTel_no(tel_no);
		String describes = request.getParameter("describes");// 站点描述
		station.setDescribes(describes);	
		String discover_user_id = request.getParameter("discover_user_id");//    
		if(StringUtil.isNotBlank(discover_user_id)){
			station.setDiscover_user_id(discover_user_id);
		}
		String pay_manner = request.getParameter("pay_manner");//     
		if(StringUtil.isNotBlank(pay_manner)){
			station.setPay_manner(pay_manner);
		}
		String verify_flag_str = request.getParameter("verify_flag");//     
		if(StringUtil.isNumeric(verify_flag_str)){
			Integer verify_flag = Integer.valueOf(verify_flag_str);
			station.setVerify_flag(verify_flag);
		}
		String valid_flag_str = request.getParameter("valid_flag");// 有效标记
		if(StringUtil.isNotBlank(valid_flag_str)){
			Integer valid_flag = Integer.valueOf(valid_flag_str);
			station.setValid_flag(valid_flag);
		}
		return stationExternalService.updateStation(station);
	}
	
	// 17.第三方站点删除接口
	@SuppressWarnings("rawtypes")
	@RequestMapping("/logicalDelStationExternal")
	@ResponseBody
	public JsonForList logicalDelStationExternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JsonForList isLogin = StringUtil.isLogin(request);
		if(isLogin.getCode().equals("400")){
			return isLogin;
		}
		JsonForList json = new JsonForList();
		//如果前台页面可以传list数组就传数组，不能的话就用,做为分隔符
		String stationIds = request.getParameter("stationIdList");
		if(StringUtil.isBlank(stationIds)){
			json.setCode("400");
			json.setMsg("stationIdList null");
			return json;
		}
		List<String> stationIdList = JSON.parseArray(stationIds,String.class);
		return stationExternalService.deleteStation(stationIdList);
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/exportStationExternal")
	@ResponseBody
	public JsonForList exportStationExternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JsonForList isLogin = StringUtil.isLogin(request);
		if(isLogin.getCode().equals("400")){
			return isLogin;
		}
		String stationIds = request.getParameter("stationIdList");
		List<OmsStationExternal> list;
		if(StringUtil.isBlank(stationIds)){
			String province_code = request.getParameter("province_code");//district_code
			String city_code = request.getParameter("city_code");
			String district_code = request.getParameter("district_code");
			if(StringUtil.isBlank(province_code)||StringUtil.isBlank(city_code)||StringUtil.isBlank(district_code)){
				//查询全部
				list =  stationExternalService.findAll();
			}else{
				//按地区条件查询
				list =  stationExternalService.getStationByDistrictCode(province_code, city_code, district_code);
			}
		}else{
			//按站点ID查询
			list = stationExternalService.getStationByIds(stationIds);
		}
		//生成excel文件并提示下载
		String[] Title={"站级编号","原始站级编号","数据来源序列号","站点名称","充电运营服务商ID","区县代码","商圈/楼盘","地图类型","坐标","停车处","停车处大体位置","环境照片URL路径","总电桩数",
				"建立时间","站点类型","是否可预约","详细地址","当前状态","直流桩数量","交流桩数量","电话","站点描述","停车费参考价","充电费参考价","充电支付方式","验证标记","探索者用户ID","有效标记"};
		String fileName = StringUtil.now14time()+"_oms_station_external.xls";
		List<Object> li = new ArrayList<Object>() ;
		for(OmsStationExternal station : list){
			li.add(station);
		}
//		ExcelExporter.exportExcel(fileName, Title, li, response); TODO luoyp
		return null;
	}
	
}
